package com.wn.service;

import com.common.base.BaseService;
import com.common.entity.Num;
import com.common.entity.PageParam;
import com.common.entity.Rsp;
import com.common.entity.UserCoupon;
import com.github.pagehelper.PageInfo;
import com.wn.mapper.UserCouponMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class UserCouponService implements BaseService<UserCoupon> {
    @Resource
    private UserCouponMapper userCouponMapper;

    @Override
    public PageInfo<UserCoupon> searchSomeByPage(PageParam<UserCoupon> pageParam) {

        return null;
    }

    @Override
    public UserCoupon getOneById(String id) {
        UserCoupon oneById = userCouponMapper.getOneById(id);
        return oneById;
    }

    @Override
    public Rsp addOne(UserCoupon userCoupon) {
        userCoupon.setId(UUID.randomUUID().toString().replace("-",""));
        int i = userCouponMapper.addOne(userCoupon);
        if(i>0){
            return Rsp.success(userCoupon);
        }
        return Rsp.fail();
    }

    @Override
    public Rsp update(UserCoupon userCoupon) {
        int i = userCouponMapper.update(userCoupon);
        if(i>0){
            return Rsp.success(userCouponMapper.getOneById(userCoupon.getId()));
        }
        return Rsp.fail();
    }

    @Override
    public Rsp deleteLogical(UserCoupon userCoupon) {
        userCoupon.setDelected(Num.DELETED_TRUE);
        int i = userCouponMapper.update(userCoupon);
        if(i>0){
            return Rsp.success(userCoupon);
        }
        return Rsp.fail();
    }

    public Rsp deleteTrueUserCoupon(UserCoupon userCoupon) {
        int i = userCouponMapper.deleteOneTrue(userCoupon.getId());
        if(i>0){
            return Rsp.success(userCoupon);
        }
        return Rsp.fail();
    }

    public Rsp searchList(String id) {
     List<UserCoupon> userCoupons = userCouponMapper.searchList(id);
     return StringUtils.isEmpty(userCoupons)?Rsp.fail():Rsp.success(userCoupons);
    }
}
